package com.ruoyi.game.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.account.domain.Account;
import com.ruoyi.account.domain.AccountTrade;
import com.ruoyi.account.mapper.AccountMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.GameConst;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.GameEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.common.utils.UserContext;
import com.ruoyi.game.domain.bo.WingoReqBO;
import com.ruoyi.game.domain.entity.UserWingoRecord;
import com.ruoyi.game.mapper.UserWingoRecordMapper;
import com.ruoyi.game.service.IRoundService;
import com.ruoyi.game.service.IUserWingoRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * 用户wingo投注信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class UserWingoRecordServiceImpl implements IUserWingoRecordService
{
    @Resource
    private UserWingoRecordMapper userWingoRecordMapper;
    @Resource
    private IRoundService roundService;
    @Resource
    private AccountMapper accountMapper;
    @Override
    public AjaxResult go(WingoReqBO wingoReqBO) {
        Date now = DateUtil.date();
        // 校验场次有效性
        AjaxResult ajaxResult = roundService.checkRound(now,wingoReqBO.getRoundNo(), wingoReqBO.getTimeLimit(), GameEnum.GameTypeEnum.WINGO.getType());
        if(ajaxResult.isError()){
            return ajaxResult;
        }
        BigDecimal total = NumberUtil.mul(wingoReqBO.getMoney(),wingoReqBO.getX());
        // TODO 校验用户余额

        // 添加用户投注记录
        UserWingoRecord userWingoRecord = new UserWingoRecord();
        userWingoRecord.setUserId(UserContext.getUserId());
        userWingoRecord.setRoundNo(wingoReqBO.getRoundNo());
        userWingoRecord.setPlayType(wingoReqBO.getPlayType());
        userWingoRecord.setPlayValue(wingoReqBO.getBallNo());
        userWingoRecord.setTimeLimit(wingoReqBO.getTimeLimit());
        userWingoRecord.setAmount(wingoReqBO.getMoney());
        userWingoRecord.setMulX(wingoReqBO.getX());
        userWingoRecord.setCreateTime(DateUtils.getNowDate());
        BigDecimal fee = NumberUtil.mul(total, GameConst.FEE_RATE);
        userWingoRecord.setTotalAmount(NumberUtil.sub(total,fee));
        userWingoRecordMapper.insertUserWingoRecord(userWingoRecord);
        return AjaxResult.success();
    }

    @Override
    public List<UserWingoRecord> getNotEndWingoRecord(String roundNo) {
        return userWingoRecordMapper.findNotEndWingoRecord(roundNo);
    }

    @Override
    public boolean updateBatch(List<UserWingoRecord> userWingoRecords) {
        for(UserWingoRecord userWingoRecord : userWingoRecords){
            System.out.println("userWingoRecords用户个数"+userWingoRecords.size());
            userWingoRecordMapper.updateUserWingoRecord(userWingoRecord);
            //更改账户可提现金额
            Account account = new Account();
            account.setUserId(userWingoRecord.getUserId());
            account.setWithdrawableAccount(userWingoRecord.getWinAmount());
            System.out.println("中奖金额"+userWingoRecord.getWinAmount());
            accountMapper.upUserAccountMessage(account);
            //更改账户流水信息
            AccountTrade accountTrade = new AccountTrade();
            accountTrade.setUserId(userWingoRecord.getUserId());
            accountTrade.setTradeType("中奖");  //交易类型
            accountTrade.setTradeState(1);  //交易状态
            accountTrade.setCreateTimes(DateUtils.getNowDate());  //创建时间
            accountTrade.setRemark("中奖"); //交易说明
            //插入一条流水信息
            accountMapper.insertUserAccountTradeMessage(accountTrade);

        }
        return true;
    }

    @Override
    public List<UserWingoRecord> page(Long userId, int pageNo, int pageSize,int timeLimit) {
        PageHelper.startPage(pageNo,pageSize,false);
        return userWingoRecordMapper.page(userId,timeLimit);
    }

    @Override
    public UserWingoRecord findByRoundNo(Long userId, String roundNo) {
        UserWingoRecord userWingoRecord = new UserWingoRecord();
        userWingoRecord.setUserId(userId);
        userWingoRecord.setRoundNo(roundNo);
        List<UserWingoRecord> list = userWingoRecordMapper.selectUserWingoRecordList(userWingoRecord);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}
